package cz.proks.easyhelpdesk.gae.api.impl.ws;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cz.proks.easyhelpdesk.api.ThreadApi;
import cz.proks.easyhelpdesk.api.config.Configuration;
import cz.proks.easyhelpdesk.api.exception.ApiException;
import cz.proks.easyhelpdesk.api.model.Thread;

@Controller(Configuration.WS.THREAD_API)
@RequestMapping(Configuration.WS.THREAD_API_BASE_URL)
public class ThreadApiImpl extends BaseApiImpl<Thread> implements ThreadApi {    
    private final Logger log = LoggerFactory.getLogger(getClass());
    
    @Autowired
    @Qualifier(Configuration.Core.WS.THREAD_API)
    private ThreadApi coreApi;
    
    @Override
    public ThreadApi getCoreApi() {
        return coreApi;
    }
    
    @Override
    @RequestMapping(value="/listForHelpdeskMember/{helpdeskMemberId}/withPaging/{from}-{to}", method = RequestMethod.GET)
    @ResponseBody
    public List<Thread> listForHelpdeskMemberWithPaging(@PathVariable Long helpdeskMemberId, @PathVariable Long from, @PathVariable Long to) throws ApiException {
        log.debug("listForHelpdeskMemberWithPaging():: Start with helpdeskMemberId '{}', from '{}', to '{}'", new Object[] {helpdeskMemberId, from, to});
        
        List<Thread> entities = getCoreApi().listForHelpdeskMemberWithPaging(helpdeskMemberId, from, to);
        
        log.debug("listForHelpdeskMemberWithPaging():: End with {}", (entities == null ? "null list" : "list of " + entities.size() + " elements"));
        return entities;
    }

    @Override
    @RequestMapping(value = "/{id}/addTicket/{ticketId}", method = RequestMethod.PUT)
    @ResponseBody
    public void addTicket(@PathVariable Long id, @PathVariable Long ticketId) throws ApiException {
        log.debug("addTicket():: Start with id '{}', ticketId '{}'", id, ticketId);
        
        getCoreApi().addTicket(id, ticketId);
        
        log.debug("addTicket():: Exit");
    }

    @Override
    @RequestMapping(value = "/{id}/removeTicket/{ticketId}", method = RequestMethod.DELETE)
    @ResponseBody
    public void removeTicket(@PathVariable Long id, @PathVariable Long ticketId) throws ApiException {
        log.debug("removeTicket():: Start with id '{}', ticketId '{}'", id, ticketId);
        
        getCoreApi().removeTicket(id, ticketId);
        
        log.debug("removeTicket():: Exit");
    }            
}
